package com.zyd.module.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zyd.enums.GlobalConstant;
import com.zyd.module.admin.mapper.ProductImageMapper;
import com.zyd.module.admin.mapper.ProductInfosMapper;
import com.zyd.module.admin.mapper.ProductTypeMapper;
import com.zyd.module.admin.model.ProductImage;
import com.zyd.module.admin.model.ProductInfos;
import com.zyd.module.admin.model.ProductType;
import com.zyd.module.admin.service.ProductInfosService;
import com.zyd.module.admin.vo.ProductInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author tiezhu
 * @date 2022年4月6日
 */
@Service("productInfosService")
public class ProductInfosServiceImpl extends ServiceImpl<ProductInfosMapper, ProductInfos> implements ProductInfosService {

    @Autowired
    private ProductInfosMapper productInfosMapper;

    @Autowired
    private ProductTypeMapper  productTypeMapper;

    @Autowired
    private ProductImageMapper productImageMapper;
    @Override
    public List<ProductInfos> selectProductInfosByTypeId(Integer fid) {
        QueryWrapper<ProductInfos> queryWrapper=new QueryWrapper<>();
        if (fid!=null||fid!=0){
            queryWrapper.eq("type_id",fid);
            return productInfosMapper.selectList(queryWrapper);
        }else {
            return null;
        }

    }

    @Override
    public List<ProductInfos> findProductPages(IPage<ProductInfos> page, ProductInfoVo infoVo) {
        QueryWrapper<ProductInfos> queryWrapper=new QueryWrapper<>();
        if (infoVo.getProName()!=null&&infoVo.getProName()!=""){
            queryWrapper.like("pro_name",infoVo.getProName());
        }
        if (infoVo.getTypeId()!=null&&infoVo.getTypeId()!=0){
            queryWrapper.eq("type_id",infoVo.getTypeId());
        }
        if (infoVo.getIslock()!=null&&(infoVo.getIslock()==0||infoVo.getIslock()==1)){
            queryWrapper.eq("islock",infoVo.getIslock());
        }
        queryWrapper.orderByDesc("update_time");
        IPage<ProductInfos> productInfosIPage = productInfosMapper.selectPage(page, queryWrapper);
        List<ProductInfos> records = productInfosIPage.getRecords();
        QueryWrapper<ProductImage> imageQueryWrapper=new QueryWrapper<>();
        for (ProductInfos infos:records) {
            imageQueryWrapper.eq("img_type", GlobalConstant.PRODUCT_THUMBNAIL);
            imageQueryWrapper.eq("pro_id",infos.getId());
            ProductImage one = productImageMapper.selectOne(imageQueryWrapper);
            ProductType productType = productTypeMapper.selectById(infos.getTypeId());
            if (one!=null){
                infos.setImgUrl(one.getProImg());
            }else {
                infos.setImgUrl(GlobalConstant.IMAGE_LOSS);
            }
            infos.setProductType(productType);
            imageQueryWrapper.clear();
        }

        return records;
    }

    @Override
    public Integer removeProductrByIds(List<String> asList) {
        return productInfosMapper.deleteBatchIds(asList);
    }

    @Override
    public ProductInfos getAndThumbnailById(String id) {
        ProductInfos productInfos = productInfosMapper.selectById(id);
        QueryWrapper<ProductImage> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("img_type",GlobalConstant.PRODUCT_THUMBNAIL);
        queryWrapper.eq("pro_id",id);
        ProductImage thumbnail = productImageMapper.selectList(queryWrapper).get(0);
        if (thumbnail!=null){
            productInfos.setImgUrl(thumbnail.getProImg());
        }
        ProductType productType = productTypeMapper.selectById(productInfos.getTypeId());
        if (productType!=null){
            productInfos.setProductType(productType);
        }
        return productInfos;
    }

    @Override
    public Long getProductLookCount() {
        QueryWrapper<ProductInfos> queryWrapper=new QueryWrapper<>();
        queryWrapper.select("look_count");
        Long count=0L;
        List<ProductInfos> productInfosList = productInfosMapper.selectList(queryWrapper);
        for (ProductInfos infos:productInfosList) {
            count=count+infos.getLookCount();
        }
        return count;
    }

    /**
     * 根据类别id获取产品信息
     * @param typeId
     * @return  List<ProductInfos>
     */
    @Override
    public List<ProductInfos> getProductsInfoListByType(Integer typeId,IPage<ProductInfos> page) {
        QueryWrapper<ProductInfos> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("type_id",typeId);
        queryWrapper.orderByDesc("update_time");
        IPage<ProductInfos> productInfosIPage = productInfosMapper.selectPage(page, queryWrapper);
        List<ProductInfos> records = productInfosIPage.getRecords();
        QueryWrapper<ProductImage> imageQueryWrapper=new QueryWrapper<>();
        for (ProductInfos infos:records) {
            imageQueryWrapper.eq("img_type", GlobalConstant.PRODUCT_THUMBNAIL);
            imageQueryWrapper.eq("pro_id",infos.getId());
            ProductImage one = productImageMapper.selectOne(imageQueryWrapper);
            ProductType productType = productTypeMapper.selectById(infos.getTypeId());
            if (one!=null){
                infos.setImgUrl(one.getProImg());
            }else {
                infos.setImgUrl(GlobalConstant.IMAGE_LOSS);
            }
            infos.setProductType(productType);
            imageQueryWrapper.clear();
        }
        return records;
    }
}